package com.fernfx.xingtan.common.pullrefresh;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.AppManager;
import com.fernfx.xingtan.common.base.BaseAdapter;
import com.fernfx.xingtan.my.entity.HaveDomainEntity;
import com.fernfx.xingtan.my.entity.MyPocketMoneyHistroyEntity;
import com.fernfx.xingtan.my.entity.MyPrincipalEntity;
import com.fernfx.xingtan.my.entity.PurchaseHistroyEntity;
import com.fernfx.xingtan.my.entity.RetrunDomainEntity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.user.ui.LoginActivity;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.UIUtil;
import com.fernfx.xingtan.view.dialog.AlertDialog;

import java.util.List;

import static com.fernfx.xingtan.common.pullrefresh.PullToRefreshActivity.HAVE_DOMAIN_STATUS;
import static com.fernfx.xingtan.common.pullrefresh.PullToRefreshActivity.MY_POCKET_MONEY_STATUS;
import static com.fernfx.xingtan.common.pullrefresh.PullToRefreshActivity.MY_PRINCIPAL_STATUS;
import static com.fernfx.xingtan.common.pullrefresh.PullToRefreshActivity.PURCHASE_HISTROY_STATUS;
import static com.fernfx.xingtan.common.pullrefresh.PullToRefreshActivity.RETRUN_DOMAIN_STATUS;

/**
 * Created by admin on 2018/8/20.
 */

public class PullToRefreshAdapter extends BaseAdapter {
    private List dataList;
    private ButtonDisplay buttonDisplay;
    private Context context;

    public PullToRefreshAdapter(Context context, List dataList) {
        super(context, dataList);
        this.dataList = dataList;
        this.context = context;
    }

    public void setButtonDisplay(ButtonDisplay buttonDisplay) {
        this.buttonDisplay = buttonDisplay;
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_pull_refresh;
    }

    @Override
    public Holder initHolder(View container) {
        PullToRefreshHolder holder = new PullToRefreshHolder();
        holder.title1TextTv = container.findViewById(R.id.title1_text_tv);
        holder.title1ContentTv = container.findViewById(R.id.title1_content_tv);
        holder.title2TextTv = container.findViewById(R.id.title2_text_tv);
        holder.title2ContentTv = container.findViewById(R.id.title2_content_tv);
        return holder;
    }

    @Override
    public void initItem(View view, Holder holder, int position) {
        PullToRefreshHolder pullToRefreshHolder = (PullToRefreshHolder) holder;
        switch (buttonDisplay.getStatus()) {
            case MY_POCKET_MONEY_STATUS:
                List<MyPocketMoneyHistroyEntity.ObjBean.RecordsBean> recordsBeansList = (List<MyPocketMoneyHistroyEntity.ObjBean.RecordsBean>) dataList;
                MyPocketMoneyHistroyEntity.ObjBean.RecordsBean recordsBean = recordsBeansList.get(position);
                //set value
                pullToRefreshHolder.title1TextTv.setText(OtherUtil.checkEmptyDefault(recordsBean.getGetOrPay()));
                pullToRefreshHolder.title1ContentTv.setText(OtherUtil.checkEmptyDefault(recordsBean.getCreateTime()));
                pullToRefreshHolder.title2TextTv.setText("￥" + OtherUtil.checkEmptyDefault(recordsBean.getAmount()));
                pullToRefreshHolder.title2ContentTv.setText(OtherUtil.checkEmptyDefault(recordsBean.getAbsAmount()) + "元");
                break;
            case MY_PRINCIPAL_STATUS:
                List<MyPrincipalEntity.ObjBean.RecordsBean> principalList = (List<MyPrincipalEntity.ObjBean.RecordsBean>) dataList;
                MyPrincipalEntity.ObjBean.RecordsBean principalBean = principalList.get(position);
                //set value
                pullToRefreshHolder.title1TextTv.setText("收到");
                pullToRefreshHolder.title1ContentTv.setText(OtherUtil.checkEmptyDefault(principalBean.getDate()));
                pullToRefreshHolder.title2TextTv.setText("￥" + OtherUtil.checkEmptyDefault("" + principalBean.getAmount()));
                pullToRefreshHolder.title2ContentTv.setText(OtherUtil.checkEmptyDefault("+" + principalBean.getAmount()) + "元");
                break;
            case PURCHASE_HISTROY_STATUS:
                List<PurchaseHistroyEntity.ObjBean.RecordsBean> purchaseHistroyList = (List<PurchaseHistroyEntity.ObjBean.RecordsBean>) dataList;
                PurchaseHistroyEntity.ObjBean.RecordsBean purchaseHistroyBean = purchaseHistroyList.get(position);
                pullToRefreshHolder.title1TextTv.setText("购买");
                pullToRefreshHolder.title1ContentTv.setText(OtherUtil.checkEmptyDefault(purchaseHistroyBean.getDate()));
                pullToRefreshHolder.title2TextTv.setText(OtherUtil.checkEmptyDefault(purchaseHistroyBean.getName()));
                pullToRefreshHolder.title2ContentTv.setVisibility(View.GONE);
                break;
            case HAVE_DOMAIN_STATUS:
                //拥有的领土
                List<HaveDomainEntity.ObjBean.RecordsBean> haveDomainList = (List<HaveDomainEntity.ObjBean.RecordsBean>) dataList;
                final HaveDomainEntity.ObjBean.RecordsBean haveDomainBean = haveDomainList.get(position);
                pullToRefreshHolder.title1TextTv.setText("拥有");
                pullToRefreshHolder.title1ContentTv.setText(haveDomainBean.getDate());
                pullToRefreshHolder.title2TextTv.setText(OtherUtil.checkEmptyDefault(haveDomainBean.getName()));
                //status
                int status = haveDomainBean.getState();
                if (status == 1) {
                    pullToRefreshHolder.title2ContentTv.setText("退还");
                    pullToRefreshHolder.title2ContentTv.setTextColor(UIUtil.getColor(R.color.green));
                    pullToRefreshHolder.title2ContentTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PullToRefreshActivity pullToRefreshActivity = (PullToRefreshActivity) context;
                            pullToRefreshActivity.presenter.returnDomain(haveDomainBean.getId() + "", haveDomainBean, (TextView) v);
                        }
                    });
                } else if (status == 2) {
                    pullToRefreshHolder.title2ContentTv.setText("待审核");
                }
                break;
            case RETRUN_DOMAIN_STATUS:
                //拥有的领土
                List<RetrunDomainEntity.ObjBean.RecordsBean> returnDomainList = (List<RetrunDomainEntity.ObjBean.RecordsBean>) dataList;
                final RetrunDomainEntity.ObjBean.RecordsBean returnDomainBean = returnDomainList.get(position);
                if (returnDomainBean.getStatus() == 1) {
                    pullToRefreshHolder.title1TextTv.setText("已经退租");
                }else if(returnDomainBean.getStatus() == 3) {
                    pullToRefreshHolder.title1TextTv.setText("未通过");
                }
                pullToRefreshHolder.title1ContentTv.setText(returnDomainBean.getTime());
                pullToRefreshHolder.title2TextTv.setText(OtherUtil.checkEmptyDefault(returnDomainBean.getName()));
                pullToRefreshHolder.title2ContentTv.setVisibility(View.GONE);
                break;
        }
    }

    class PullToRefreshHolder extends Holder {
        TextView title1TextTv;
        TextView title1ContentTv;
        TextView title2TextTv;
        TextView title2ContentTv;
    }

    interface ButtonDisplay {

        int getStatus();
    }
}
